#include "httplib.h"
#define SERVER_IP "119.3.231.9"


void Hello(const httplib::Request &req, httplib::Response &rsp)
{
    rsp.set_content("Hello World!", "text/plain");
    rsp.status = 200;
}

void Numbers(const httplib::Request &req, httplib::Response &rsp)
{
    auto num = req.matches[1];   //0里面存的是整体路径，往后下标保存的是捕捉的数据
    rsp.set_content(num, "text/plain");
    rsp.status = 200;
}
void Multipart(const httplib::Request &req, httplib::Response &rsp)
{
    auto ret = req.has_file("file");
    if(ret == false)
    {
        std::cout<<"not file upload\n";
        rsp.status = 400;
        return;
    }
    const auto& file = req.get_file_value("file");
    rsp.body.clear();
    rsp.body = file.filename;   //文件名称
    rsp.body += "\n";
    rsp.body += file.content;   //文件内容
    rsp.set_header("Content-Type", "text/plain");
    rsp.status = 200;
}

int main()
{
    httplib::Server server;  //实例化Server对象，用来搭建服务器

    server.Get("/hi", Hello);  //注册一个针对/hi的Get请求的处理函数映射关系
    server.Get(R"(/numbers/(\d+))",Numbers);
    server.Post("/multipart", Multipart);

    server.listen("0.0.0.0", 8888);

    return 0;
}




// #include "httplib.h"
// int main(void)
// {
// using namespace httplib;
// Server svr;
// svr.Get("/hi", [](const Request& req, Response& res) {
// res.set_content("Hello World!", "text/plain");
// });
// svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
// auto numbers = req.matches[1];
// res.set_content(numbers, "text/plain");
// });
// svr.Post("/upload", [&](const auto& req, auto& res) {
// auto size = req.files.size();
// auto ret = req.has_file("file1");
// const auto& file = req.get_file_value("file1");
// std::cout << file.filename << std::endl;
// std::cout << file.content_type << std::endl;
// std::cout << file.content << std::endl;
// });
// svr.listen("0.0.0.0", 8888);
// return 0;
// }